//
//  MSRangeSlider.m
//  JYG_Agent
//
//  Created by apple on 15/10/21.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "MSRangeSlider.h"

@interface MSRangeSlider ()
{
    CGFloat minValue;
    CGFloat maxValue;
    CGFloat leftValue;
    CGFloat rightValue;
    UIImageView *sliderImage;
    UIImageView *rangeImage;
    
    UIImageView *leftThumb;
    UIImageView *rightThumb;
    
    UILabel *leftValueLabel;
    UILabel *rightValueLabel;
}
@end
@implementation MSRangeSlider
@dynamic minValue, maxValue, leftValue, rightValue;

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup {
    if (maxValue == 0.0) {
        maxValue = 100.0;
    }
    
    leftValue = minValue;
    rightValue = maxValue;
    
    sliderImage = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"dot"] stretchableImageWithLeftCapWidth:0.5 topCapHeight:0.5]];
    [self addSubview:sliderImage];
    
    rangeImage = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"greendot"] stretchableImageWithLeftCapWidth:0.5 topCapHeight:0.5]];
    [self addSubview:rangeImage];
    
    leftThumb = [[UIImageView alloc] initWithFrame:CGRectMake(0, 30-MSRangeSlider_THUMB_SIZE/2.0, MSRangeSlider_THUMB_SIZE, MSRangeSlider_THUMB_SIZE)];
    leftThumb.image = [UIImage imageNamed:@"sliderThumb"];
    leftThumb.userInteractionEnabled = YES;
    leftThumb.contentMode = UIViewContentModeCenter;
    [self addSubview:leftThumb];
    
    UIPanGestureRecognizer *leftPan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftPan:)];
    [leftThumb addGestureRecognizer:leftPan];
    
    rightThumb = [[UIImageView alloc] initWithFrame:CGRectMake(0, 30-MSRangeSlider_THUMB_SIZE/2.0, MSRangeSlider_THUMB_SIZE, MSRangeSlider_THUMB_SIZE)];
    rightThumb.image = [UIImage imageNamed:@"sliderThumb"];
    rightThumb.userInteractionEnabled = YES;
    rightThumb.contentMode = UIViewContentModeCenter;
    [self addSubview:rightThumb];
    
    UIPanGestureRecognizer *rightPan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightPan:)];
    [rightThumb addGestureRecognizer:rightPan];
    
    leftValueLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 20)];
    leftValueLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:15];
    leftValueLabel.textColor = kBlueColor;
    leftValueLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:leftValueLabel];
    
    rightValueLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.right-40, 0, 40, 20)];
    rightValueLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:15];
    rightValueLabel.textColor = kBlueColor;
    rightValueLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:rightValueLabel];
}

- (void)layoutSubviews {
    
    CGFloat availableWidth = self.frame.size.width-MSRangeSlider_THUMB_SIZE;
    CGFloat inset = MSRangeSlider_THUMB_SIZE / 2;
    
    CGFloat range = maxValue - minValue;
    
    CGFloat left = floorf((leftValue - minValue) / range * availableWidth);
    CGFloat right = floorf((rightValue - minValue) / range * availableWidth);
    
    if (isnan(left)) {
        left = 0;
    }
    
    if (isnan(right)) {
        right = 0;
    }
    sliderImage.frame = CGRectMake(inset, 30, availableWidth, 5);
    CGFloat rangeWidth = right - left;
    rangeImage.frame = CGRectMake(inset + left, 30, rangeWidth, 5);
    
    leftThumb.center = CGPointMake(inset + left, 30 + sliderImage.height / 2);
    rightThumb.center = CGPointMake(inset + right, 30 + sliderImage.height / 2);
    
    leftValueLabel.center = CGPointMake(inset + left, 10);
    rightValueLabel.center = CGPointMake(inset + right, 10);
    
    leftValueLabel.text = [NSString stringWithFormat:@"%d",(int)leftValue];
    NSString *rightLabelText;
    if (rightValue>=maxValue) {
        rightLabelText = @"不限";
    } else {
        rightLabelText = [NSString stringWithFormat:@"%d",(int)rightValue];
    }
    rightValueLabel.text = rightLabelText;
}

#pragma mark - actions
- (void)handleLeftPan:(UIPanGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
        CGPoint translation = [gesture translationInView:self];
        CGFloat range = maxValue - minValue;
        CGFloat availableWidth = self.frame.size.width - MSRangeSlider_THUMB_SIZE;
        self.leftValue += translation.x / availableWidth * range;
        
        [gesture setTranslation:CGPointZero inView:self];
        
        [self sendActionsForControlEvents:UIControlEventValueChanged];
    }
}

- (void)handleRightPan:(UIPanGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
        CGPoint translation = [gesture translationInView:self];
        CGFloat range = maxValue - minValue;
        CGFloat availableWidth = self.frame.size.width - MSRangeSlider_THUMB_SIZE;
        self.rightValue += translation.x / availableWidth * range;
        
        [gesture setTranslation:CGPointZero inView:self];
        
        [self sendActionsForControlEvents:UIControlEventValueChanged];
    }
}

#pragma mark - setters
- (void)setLeftValue:(CGFloat)newValue {
    if (newValue < minValue)
        newValue = minValue;
    
    CGFloat availableWidth = self.frame.size.width-MSRangeSlider_THUMB_SIZE;
    CGFloat minInterval = (MSRangeSlider_THUMB_SIZE+10)/availableWidth*floorf(maxValue - minValue);
    if (newValue > rightValue-minInterval)
        newValue = rightValue-minInterval;
    
    leftValue = newValue;
    
    [self setNeedsLayout];
}

- (void)setRightValue:(CGFloat)newValue {
    if (newValue > maxValue)
        newValue = maxValue;
    
    CGFloat availableWidth = self.frame.size.width-MSRangeSlider_THUMB_SIZE;
    CGFloat minInterval = (MSRangeSlider_THUMB_SIZE+10)/availableWidth*floorf(maxValue - minValue);
    if (newValue < leftValue+minInterval)
        newValue = leftValue+minInterval;
    
    rightValue = newValue;
    
    [self setNeedsLayout];
}

- (void)setMinValue:(CGFloat)newValue {
    minValue = newValue;
    leftValue = minValue;
    [self setNeedsLayout];
}

- (void)setMaxValue:(CGFloat)newValue {
    maxValue = newValue;
    rightValue = maxValue;
    [self setNeedsLayout];
}

#pragma mark - getters
- (CGFloat)minValue {
    return minValue;
}

- (CGFloat)maxValue {
    return maxValue;
}

- (CGFloat)leftValue {
    return leftValue;
}

- (CGFloat)rightValue {
    return rightValue;
}

@end
